package org.xfy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xfy.base.util.EmailUtil;
import org.xfy.base.util.ExcelUtils;
import org.xfy.org.domain.dto.ShopDto;
import org.xfy.org.domain.dto.ShopRejectDto;
import org.xfy.org.query.ShopQuery;
import org.xfy.org.domain.Shop;
import org.xfy.org.service.IShopService;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API", description = "店铺相关的CRUD功能")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @GetMapping("/echarts")
    public AjaxResult echarts(){
        return shopService.echarts();
    }
    /**
     * 导入
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 1, 1, Shop.class);
        for (Shop shop : shops) {
            shopService.add(shop);
        }
    }
    /**
     * 导出功能
     * @param
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> all = shopService.findAll();
        ExcelUtils.exportExcel(all,"店铺信息","sheet1",Shop.class,"店铺信息导出",response);
    }
    @GetMapping("/activation/{shopId}")
    @ApiOperation(value = "激活店铺")
    public AjaxResult activation(@PathVariable("shopId") Long shopId){
        return shopService.activation(shopId);
    }

    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过")
    public AjaxResult pass(@RequestBody ShopRejectDto shopRejectDto){
        return shopService.pass(shopRejectDto);
    }
    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回")
    public AjaxResult reject(@RequestBody ShopRejectDto shopRejectDto){
        return shopService.reject(shopRejectDto);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@Valid @RequestBody ShopDto shopDto){
        return shopService.settlement(shopDto);
    }

    @PostMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult save(@RequestBody Shop shop) {
        if (shop.getId() == null) {//新增
            shopService.add(shop);
        } else {
            shopService.update(shop);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public AjaxResult del(@PathVariable("id") Long id) {
        shopService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public AjaxResult PatchDel(@RequestBody Long[] ids) {
        shopService.patchDel(ids);
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult findOne(@PathVariable("id") Long id) {
        Shop shop = shopService.findOne(id);
        return AjaxResult.me().setResultObj(shop);
    }

    @GetMapping()
    @ApiOperation(value = "查询所有")
    public AjaxResult findAll() {
        List<Shop> shops = shopService.findAll();
        return AjaxResult.me().setResultObj(shops);
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody ShopQuery query) {
        PageList<Shop> pageList = shopService.queryPage(query);
        return AjaxResult.me().setResultObj(pageList);
    }

}
